ISR(TIMER0_OVF_vect) {

time++;

   if (time==488)     {#asm("wdr");time=0;seconds++;}
   if (seconds==60)   {seconds=0; minutes++;  }
   if (minutes==60)   {minutes=0; hours++;    }
  
   if(minutes==1){
PORT_ON;
}
if(minutes==2){
PORT_OFF;
}
   
 }

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <string.h>
#include <string.h>
#include <util/delay.h>

volatile unsigned char  hours,minutes,seconds; 
volatile unsigned int time; 

#define PORT 6
#define PORT_ON PORTB|=1<<PORT
#define PORT_OFF PORTB&=~(1<<PORT)

ISR(TIMER0_OVF_vect) {

time++;

   if (time==488)     {asm#("wdr");time=0;seconds++;}
   if (seconds==60)   {seconds=0; minutes++;  }
   if (minutes==60)   {minutes=0; hours++;    }

}


ISR(TIMER1_OVF_vect) {
   if(minutes==01){
PORT_ON;
}
   if(minutes==02){
PORT_OFF;
}
   if(minutes==03){
PORT_ON;
}
   if(minutes==04){
PORT_OFF;
   } 
}   

int main(void)
{
// Input/Output Ports initialization
PORTB=0xFF;
DDRB=0x00;

// Port C initialization
PORTC=0xFF;
DDRC=0x00;

// Port D initialization
PORTD=0xFF;
DDRD=0x00;

DDRB|=(1<<6); PORT_OFF;


// External Interrupt(s) initialization
// INT0: On
EICRA=0x00;
EIMSK=0x00;
EIFR=0x00;
PCICR=0x00;


// Clock value: 31.250 kHz
TCCR0A=0x00;
TCCR0B=0x02;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 1 initialization
TCCR1A=0x00;
TCCR1B=0x02;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
ASSR=0x00;
TCCR2A=0x00;
TCCR2B=0x00;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;


 // Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x01;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x01;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;

UCSR0A=0x00;
UCSR0B=0x00;
UCSR0C=0x00;
UBRR0H=0x00;
UBRR0L=0x00;


// Analog Comparator: Off
ACSR=0x80;
ADCSRB=0x00;

sei();

  while (1){     
  }
}


#include <avr/io.h>
#include <avr/eeprom.h>

unsigned char EEMEM Key[10]={'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z'};
unsigned char pas[10]; 

#define F_CPU 8000000UL
#define BAUD 9600l
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)   

void uinit (void){               

  UCSR0C |= (3<<UCSZ00);          
  //Baudrate (high und low byte)
  UBRR0H = (unsigned char)(UBRR_VAL>>8);
  UBRR0L = (unsigned char)UBRR_VAL;
  UCSR0B |= (1<<RXCIE0) | (1<<TXEN0) | (1<<RXEN0);     
}

void uputc(unsigned char c){      

   loop_until_bit_is_set(UCSR0A,UDRE0);   
   UDR0=c;                        
}

unsigned char ugetc(){         
   while (!(UCSR0A & (1<<RXC0)));   
     return UDR0;                       
}


int main(void) 
{
    uinit();

    uputc('E'); uputc('n'); uputc('t'); uputc('e');  uputc('r'); uputc(' ');
    uputc('l'); uputc('a'); uputc('s'); uputc('t');  uputc(' ');
    uputc('k'); uputc('e'); uputc('y');
    uputc(13);
    uputc(10);
    while (1)
    {
        pas[0]=ugetc(); uputc(pas[0]);
        pas[1]=ugetc(); uputc(pas[1]);
        pas[2]=ugetc(); uputc(pas[2]);
        pas[3]=ugetc(); uputc(pas[3]);
        pas[4]=ugetc(); uputc(pas[4]);
        pas[5]=ugetc(); uputc(pas[5]);
        pas[6]=ugetc(); uputc(pas[6]);
        pas[7]=ugetc(); uputc(pas[7]);
        pas[8]=ugetc(); uputc(pas[8]);
        pas[9]=ugetc(); uputc(pas[9]);
        uputc(13);
        uputc(10);
    }
}

eeprom unsigned char Key[10];

void pass() {

unsigned char pas[10]; 
 
if  (Key[6]==255) {
Key[0]='z';
Key[1]='z';
Key[2]='z';
Key[3]='z';
Key[4]='z';
Key[5]='z';
Key[6]='z'; 
Key[7]='z';
Key[8]='z';
Key[9]='z'; 

}

putchar('E'); putchar('n'); putchar('t'); putchar('e');  putchar('r'); putchar(' '); 
putchar('l'); putchar('a'); putchar('s'); putchar('t');  putchar(' ');
putchar('k'); putchar('e'); putchar('y');
putchar(13);
putchar(10);

pas[0]=getchar(); putchar('*');
pas[1]=getchar(); putchar('*');
pas[2]=getchar(); putchar('*');
pas[3]=getchar(); putchar('*');
pas[4]=getchar(); putchar('*');
pas[5]=getchar(); putchar('*');
pas[6]=getchar(); putchar('*');
pas[7]=getchar(); putchar('*');
pas[8]=getchar(); putchar('*');
pas[9]=getchar(); putchar('*');
putchar(13);
putchar(10);


if  (
pas[0]==Key[0]&&
pas[1]==Key[1]&&
pas[2]==Key[2]&& 
pas[3]==Key[3]&& 
pas[4]==Key[4]&& 
pas[5]==Key[5]&& 
pas[6]==Key[6]&& 
pas[7]==Key[7]&& 
pas[8]==Key[8]&&
pas[9]==Key[9])  

{

putchar('E'); putchar('n'); putchar('t'); putchar('e');  putchar('r'); putchar(' '); 
putchar('n'); putchar('e'); putchar('w'); putchar(' ');
putchar('k'); putchar('e'); putchar('y');
putchar(13);
putchar(10);


Key[0]=getchar(); putchar(Key[0]);
Key[1]=getchar(); putchar(Key[1]);
Key[2]=getchar(); putchar(Key[2]);
Key[3]=getchar(); putchar(Key[3]);
Key[4]=getchar(); putchar(Key[4]);
Key[5]=getchar(); putchar(Key[5]);
Key[6]=getchar(); putchar(Key[6]);
Key[7]=getchar(); putchar(Key[7]);
Key[8]=getchar(); putchar(Key[8]);
Key[9]=getchar(); putchar(Key[9]);



putchar(13);
putchar(10);

putchar('n'); putchar('e'); putchar('w'); putchar(' ');
putchar('k'); putchar('e'); putchar('y'); putchar(' ');
putchar(Key[0]);
putchar(Key[1]);
putchar(Key[2]);
putchar(Key[3]);
putchar(Key[4]);
putchar(Key[5]);
putchar(Key[6]);
putchar(Key[7]);
putchar(Key[8]);
putchar(Key[9]);
putchar(13);
putchar(10);
putchar(13);
putchar(10);
reboot();

} else {
putchar('I'); putchar('n'); putchar('c'); putchar('o'); putchar('r'); putchar('e'); putchar('c'); putchar('t'); putchar(13);putchar(10);
  }

}

#include <avr/io.h>
#include <avr/eeprom.h>

unsigned char EEMEM Key[10]={"zzzzzzzzzz"};

#define F_CPU 8000000UL
#define BAUD 9600l
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)   

void uinit (void){               

  UCSR0C |= (3<<UCSZ00);          
  //Baudrate (high und low byte)
  UBRR0H = (unsigned char)(UBRR_VAL>>8);
  UBRR0L = (unsigned char)UBRR_VAL;
  UCSR0B |= (1<<RXCIE0) | (1<<TXEN0) | (1<<RXEN0);     
}

void uputc(unsigned char c){      

   loop_until_bit_is_set(UCSR0A,UDRE0);   
   UDR0=c;                        
}

unsigned char ugetc(){         
   while (!(UCSR0A & (1<<RXC0)));   
     return UDR0;                       
}

void uputs (unsigned char *s){      //string send

  while(*s) uputc(*s++);               
}

void SetDefaultPassword()
{
   unsigned char i;
   if(eeprom_read_byte(&Key[6]) == 255) {
        for(i=0; i<10; i++) {
           eeprom_write_byte(&Key[i], 'z'); 
        }
            
    }   
}

void GetPassword()
{
    unsigned char pas[10], i;
 
    SetDefaultPassword();
    uputs((unsigned char*)"Enter last key\n\r");
    for(i=0; i<10; i++) {
        pas[i]=ugetc(); uputc('*');
    }
    uputc(13);
    uputc(10);
    if(
        pas[0]==eeprom_read_byte(&Key[0])&&
        pas[1]==eeprom_read_byte(&Key[1])&&
        pas[2]==eeprom_read_byte(&Key[2])&&
        pas[3]==eeprom_read_byte(&Key[3])&&
        pas[4]==eeprom_read_byte(&Key[4])&&
        pas[5]==eeprom_read_byte(&Key[5])&&
        pas[6]==eeprom_read_byte(&Key[6])&&
        pas[7]==eeprom_read_byte(&Key[7])&&
        pas[8]==eeprom_read_byte(&Key[8])&&
        pas[9]==eeprom_read_byte(&Key[9]) ) {

        uputs((unsigned char*)"Enter new key\n\r");
        
        for(i=0; i<10; i++) {
            eeprom_write_byte(&Key[i], ugetc());
            uputc(eeprom_read_byte(&Key[i]));
        }

        uputc(13);
        uputc(10);
        uputs((unsigned char*)"new key ");
        for(i=0; i<10; i++) {
            uputc(eeprom_read_byte(&Key[i]));
        }
        uputc(13);
        uputc(10);
    }
    else {
        
        uputs((unsigned char*)"Incorect\n\r");
    }


}

int main(void) 
{
    uinit();
    while (1)
    {
        GetPassword();        

    }
}

void activeCommand(char c[])
{  
if (c[0]=='r'&&c[1]=='e'&&c[2]=='a'&&c[3]=='d')
   {
   read();  
   }
                    
}

unsigned char i, COMMAND[4];
unsigned char i = 0;

ISR(USART_RX_vect)
{
int b;
b = UDR0;

if (b != '!')
   {
   COMMAND[i] = b;
   i++;  
   }
else
   {
   i = 0;
   activeCommand(COMMAND);
   }
} 

    'i'   
:
else{
   i = 0;
   activeCommand(COMMAND);
   c[0]='0';
   c[1]='0';
   c[2]='0';
   c[3]='0';
}

unsigned char times;
times=printf("%d:%d:%d" ,hours, minutes, seconds);
  printf? , -  .      ( 5  8,    ).     ? -  :
:
char str[9];
sprintf(str, "%02d:%02d:%02d", hr, min, sec);
for(i=0; i<8; i++) writeData(str[i]);

void writeData(unsigned char read){ 
if (page_stop_status==1)  {
i2c_start(); 
i2c_write(0xa0);
i2c_write(addr>>8); 
i2c_write(addr & 0x00FF); 
page_stop_status=0;     }

i2c_write(read); 
 
addr++; 
pageSize++;  
if (pageSize==128) {
pageSize=0; 
page_stop();
 }   
} 




#include <mega168.h>
#include <stdio.h>
#include <delay.h>



// I2C Bus functions
#asm
   .equ __i2c_port=0x08 ;PORTC
   .equ __sda_bit=0
   .equ __scl_bit=1
#endasm
#include <i2c.h>

#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

volatile unsigned int addr;
volatile unsigned char pageSize;                                 
volatile bit page_stop_status;


void meny(){
putchar(13); 
putchar(10);
printf("1.Read eeprom");
putchar(13);
putchar(10);
}

void page_stop() 
 {
i2c_stop(); 
delay_ms(30);
page_stop_status=1; 
}


void addr_save(){      //   

   i2c_start(); 
   i2c_write(0xa0);
   i2c_write(0); 
   i2c_write(0 & 0x00FF); 
                           
   i2c_write((addr>>8) & 0xFF);
   i2c_write((addr & 0xFF));   //  
   i2c_write(pageSize);        //    
   page_stop();
} 

void readaddr()        {      //   

unsigned char hi_addr,lo_addr;

i2c_start();               // I2C start signal
i2c_write(0xA0);              // send byte via I2C  (device address + W)
i2c_write(0>>8); 
i2c_write(0 & 0x00FF); 

i2c_start();               // I2C signal repeated start
i2c_write(0xA1);             // send byte (device address + R)
hi_addr = i2c_read(0); // Read the data (NO acknowledge)   


i2c_start();               // I2C signal repeated start
i2c_write(0xA1);             // send byte (device address + R)
lo_addr = i2c_read(0); // Read the data (NO acknowledge)   

i2c_start();               // I2C signal repeated start
i2c_write(0xA1);             // send byte (device address + R)
pageSize = i2c_read(0); // Read the data (NO acknowledge)   

addr=hi_addr;
addr=(addr<<8);           //     
addr=(addr | lo_addr); 

 page_stop();
 
}

void ReadMem(){          //  

unsigned int cnt;
unsigned int temp_addr;

readaddr(); 

if (addr==3) {putchar('E'); putchar('m'); putchar('t'); putchar('y');putchar(' '); putchar('M'); putchar('e'); putchar('m'); putchar('o');putchar('r'); putchar('y'); 
putchar(13);
putchar(10);
putchar(13);
putchar(10);}


else {

putchar('R'); putchar('e'); putchar('a'); putchar('d');putchar('i'); putchar('n');  putchar('g');
putchar(13);
putchar(10);
LED_ON; 


temp_addr=3;
while (temp_addr<addr) {
i2c_start(); 
i2c_write(0xa0);
i2c_write(temp_addr>>8); 
i2c_write(temp_addr & 0x00FF);   

for(cnt=0;cnt<128;cnt++)    {  
i2c_start();               // I2C signal repeated start
i2c_write(0xA1);             // send byte (device address + R)
putchar(i2c_read(0)); 
temp_addr++;
if (temp_addr==addr) 
 break;                 
} // Read the data (NO acknowledge)  
                            
page_stop();        
 }
                                       
                      
page_stop();


putchar(13);
putchar(10);
LED_OFF; 
putchar('O'); putchar('k'); 
                            } 
putchar(13);
putchar(10);
meny();        

} 

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) {

if (data=='1') ReadMem();

}
}

void main(void)
{

PORTB=0xFF;
DDRB=0x00;

// Port C initialization
PORTC=0x7F;
DDRC=0x00;

// Port D initialization

PORTD=0xE0;
DDRD=0x00;   



// External Interrupt(s) initialization
// INT0: On
EICRA=0x00;
EIMSK=0x00;
EIFR=0x00;
PCICR=0x00;

// Clock value: 31.250 kHz
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 1 initialization
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
ASSR=0x00;
TCCR2A=0x00;
TCCR2B=0x00;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x01;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x00;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;

// USART Baud Rate: 1200    
UCSR0A=0x00;
UCSR0B=0x98;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;


// Analog Comparator: Off
ACSR=0x80;
ADCSRB=0x00;
DIDR1=0x00;

// ADC disabled
ADCSRA=0x00;

// SPI disabled
SPCR=0x00;

// TWI disabled
TWCR=0x00;


/* Prepare the sleep in power save mode*/
SMCR |= ((1<<SE) | (0<<SM2) | (0<<SM1) | (0<<SM0)); 
/* Enter sleep mode */



i2c_init(); //    
delay_ms(100);


readaddr(); 
if (addr==65535) addr=pageSize=3;
addr_save();
page_stop(); 

meny();

#asm("sei") 

#pragma optsize-
#asm("wdr")
WDTCSR=0x39;
WDTCSR=0x29;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

delay_ms(100);

while (1)



    {
     page_stop(); 
     addr_save(); 

      }
}


#include < >
#define bit_is_set(reg, bit)       ((reg & (1<<(bit))) != 0)

// 
#define STROBE 0
#define DATA 1

//  
#define STROBE_HIGH PORTB|=1<<STROBE
#define STROBE_LOW PORTB&=~(1<<STROBE)
#define DATA_LOW DDRB|=1<<DATA
#define DATA_RELEASE DDRB&=~(1<<DATA)

void write(){ 
           
} 

void f2fArmToRead(){
//  
    STROBE_LOW;
    DDRB|=1<<STROBE; 
    DATA_RELEASE;
    PORTB&=~(1<<DATA);     
//  
        STROBE_HIGH;
        delay_us(3);
        DATA_LOW;
        delay_us(3);
        STROBE_LOW;
        delay_us(3);
        STROBE_HIGH;
        delay_us(3);
        STROBE_LOW;
        delay_us(3);
        DATA_RELEASE;
        delay_us(3);
        STROBE_HIGH;
        delay_us(3);
        STROBE_LOW;
        delay_us(3);
        STROBE_HIGH;
        delay_us(3);
        STROBE_LOW;
        delay_us(3);
} 


void f2fWaitData(){
        while(bit_is_set(PINB,DATA));
        delay_us(3);
        STROBE_HIGH;
        delay_us(3);
        while(!bit_is_set(PINB,DATA));
        delay_us(3);
        STROBE_LOW;
        delay_us(3); 
        LED_ON;
        while(bit_is_set(PINB,DATA));
        delay_us(3);

  }


void f2fRead176bytes(){
    char cnt,bitCnt,tmpBuf=8;    
    
   write('$');

        for(cnt=0;cnt<16;cnt++)
        	STROBE_HIGH;
		delay_us(3);
		STROBE_LOW;
		delay_us(3);  
    write('T');
    write('1');
    write(':');
        for(cnt=0;cnt<176;cnt++){  
        if(cnt==88){
    write('T'); 
    write('2'); 
    write(':'); 
        }   
                for(bitCnt=0;bitCnt<8;bitCnt++){
                STROBE_HIGH;
		delay_us(3);
		STROBE_LOW;
		delay_us(3); 
                        if(!bit_is_set(PINB,DATA)){
                                tmpBuf|=1<<(7-bitCnt); 
            }else{
                tmpBuf&=~(1<<(7-bitCnt));
            }
                } 
       if(tmpBuf!=0) { 
     writeData(tmpBuf);
}
    }  
}


void main(void)
{

#asm("sei") 

while (1)
    {  
     f2fArmToRead();
     f2fWaitData();        
     f2fRead176bytes();
      }           
}

void f2fRead176bytes(){

unsigned char bitCnt,cnt;
unsigned int tmpBuf;

    writeData('$');

        for(cnt=0;cnt<16;cnt++)
        	STROBE_HIGH;
		delay_us(3);
		STROBE_LOW;
		delay_us(3);  
    writeData('T');
    writeData('1');
    writeData(':');

    for(cnt=0;cnt<88;cnt++){  
    if(cnt==44){
        writeData('T'); 
        writeData('2'); 
        writeData(':'); 
        }

        tmpBuf=0;
        for(bitCnt=0;bitCnt<16;bitCnt++)  {
                delay_us(3); 
                tmpBuf<<=1;
            if(!(PINB & (1<<BIT)))  tmpBuf|=1;  
 }

if(tmpBuf!=0) {
writeData(tmpBuf>>8); 
writeData(tmpBuf); 
        }

    } 
    return;
}
 }   
} 
